﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using P2P_Backup.archive;
using P2P_Backup.forms.controls;

namespace P2P_Backup
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            tools.Log.Instance.setOutput(txtLog);

            backupListView.setConnections(P2PBackup.Instance.LocalBackupFileConnections);
            restoreListView.setConnections(P2PBackup.Instance.RemoteRestoreFileConnections);
            PeerBackupListView.setConnections(P2PBackup.Instance.RemoteBackupFileConnections);
            PeerRestoreListView.setConnections(P2PBackup.Instance.LocalRestoreFileConnections);

            lstPeers.SetDataSource(P2PBackup.Instance.Peers);
            trvArchive.Archive = P2PBackup.Instance.MyArchive;            
            
            Random rdn = new Random();
            int pos = rdn.Next(1, 50);
            chrtDownload.Series["dl"].Points.AddXY(0,pos);
            for (int i = 1; i < 100; i++)
            {
                int min = (pos > 25)? -2: -1;
                int max = (pos < 25)? 2 : 1;
                pos += rdn.Next(min, max);
                if (pos < 0) pos = 0;
                if (pos > 50) pos = 50;
                chrtDownload.Series["dl"].Points.AddXY(i, pos);
            }
            pos = rdn.Next(1, 50);
            chrtUpload.Series["ul"].Points.AddXY(0, pos);
            for (int i = 1; i < 100; i++)
            {
                int min = (pos > 25) ? -2 : -1;
                int max = (pos < 25) ? 2 : 1;
                pos += rdn.Next(min, max);
                if (pos < 0) pos = 0;
                if (pos > 50) pos = 50;
                chrtUpload.Series["ul"].Points.AddXY(i, pos);
            }


            //TODO: settings form maken en bij eerste opstart openen
            //TODO: Beter thread management system
            //TODO: log verder aanvullen
            //TODO: Progress Reporter
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileOk += new CancelEventHandler(OpenFileDialog_FileOk);
            ofd.Multiselect = true;
            ofd.Filter = "All files (*.*)|*.*";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            ofd.ShowDialog();
        }
        private void OpenFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            OpenFileDialog ofd = (OpenFileDialog)sender;
            if (ofd.FileNames.Length != 0)
            {
                foreach (String file in ofd.FileNames)
                {
                    trvArchive.getSelectedFolder().addFile(new File(file));
                }
            }
            P2PBackup.Instance.Save();
        }

        private void btnAddFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath != string.Empty)
            {
                trvArchive.getSelectedFolder().addFolder(new Folder(fbd.SelectedPath));
                P2PBackup.Instance.Save();
            }
        }

        private void btnCreateFolder_Click(object sender, EventArgs e)
        {
            String name = "";
            DialogResult result = InputBox.Show("Create Folder", "Type the name of the new Virtual Folder", ref name);
            if (result == DialogResult.OK && name != "")
            {
                Folder folder = new Folder()
                {
                    Name = name
                };
                trvArchive.getSelectedFolder().addFolder(folder);
                P2PBackup.Instance.Save();
            }
        }

        private void btnRemoveItem_Click(object sender, EventArgs e)
        {
            trvArchive.RemoveSelectedNode();
            P2PBackup.Instance.Save();
        }

        private void frmMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            P2PBackup.Instance.Stop();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            P2PBackup.Instance.StartBackup();
        }
        private void frmMain_Load(object sender, System.EventArgs e)
        {
            if (!System.IO.File.Exists("P2PBackup.xml"))
            {
                frmSettings settings = new frmSettings();
                settings.ShowDialog();
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            Restore.StartRestore();
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            new frmSettings().ShowDialog();
        }

    }
}
